<template>
  <div class="floating-toolball">
    <!-- 主悬浮球 -->
    <div 
      ref="toolballRef"
      class="toolball-main"
      :class="{ 'is-expanded': expanded }"
      :style="{ left: position.x + 'px', top: position.y + 'px' }"
      @mousedown="handleDragStart"
      @click="toggleExpand"
    >
      <el-icon class="toolball-icon" :class="{ 'rotate': expanded }">
        <component :is="mainIcon" />
      </el-icon>
      <div class="toolball-ripple"></div>
    </div>

    <!-- 子菜单 -->
    <transition-group name="bubble">
      <div
        v-for="(item, index) in menuItems"
        v-show="expanded"
        :key="item.path"
        class="toolball-item"
        :style="getItemPosition(index)"
        @click="handleItemClick(item)"
      >
        <el-tooltip :content="item.title" placement="left">
          <div class="item-button" :style="{ background: item.gradient }">
            <el-icon><component :is="item.icon" /></el-icon>
          </div>
        </el-tooltip>
      </div>
    </transition-group>

    <!-- 快捷操作面板 -->
    <transition name="slide-left">
      <div v-if="showQuickPanel" class="quick-panel">
        <div class="panel-header">
          <h4>快捷操作</h4>
          <el-button size="small" text @click="showQuickPanel = false">
            <el-icon><Close /></el-icon>
          </el-button>
        </div>
        <div class="panel-content">
          <div class="quick-grid">
            <div 
              v-for="action in quickActions" 
              :key="action.key"
              class="quick-action"
              @click="handleQuickAction(action)"
            >
              <div class="action-icon" :style="{ background: action.color }">
                <el-icon><component :is="action.icon" /></el-icon>
              </div>
              <span>{{ action.title }}</span>
            </div>
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onBeforeUnmount } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import {
  Plus, Close, MagicStick, Document, Box, SetUp, ShoppingCart,
  DataAnalysis, User, Setting
} from '@element-plus/icons-vue'

const router = useRouter()

// 悬浮球位置
const position = ref({
  x: window.innerWidth - 100,
  y: window.innerHeight - 200
})

// 展开状态
const expanded = ref(false)
const showQuickPanel = ref(false)

// 拖拽状态
const dragging = ref(false)
const dragOffset = ref({ x: 0, y: 0 })
const toolballRef = ref(null)

// 主图标
const mainIcon = computed(() => expanded.value ? Close : Plus)

// 菜单项
const menuItems = [
  { 
    path: '/lowcode', 
    title: '低代码平台', 
    icon: 'MagicStick',
    gradient: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)'
  },
  { 
    path: '/contract/list', 
    title: '合同管理', 
    icon: 'Document',
    gradient: 'linear-gradient(135deg, #f093fb 0%, #f5576c 100%)'
  },
  { 
    path: '/asset/list', 
    title: '资产管理', 
    icon: 'Box',
    gradient: 'linear-gradient(135deg, #4facfe 0%, #00f2fe 100%)'
  },
  { 
    path: '/equipment/ledger', 
    title: '设备管理', 
    icon: 'SetUp',
    gradient: 'linear-gradient(135deg, #43e97b 0%, #38f9d7 100%)'
  },
  { 
    path: '/sales/orders', 
    title: '销售订单', 
    icon: 'ShoppingCart',
    gradient: 'linear-gradient(135deg, #fa709a 0%, #fee140 100%)'
  },
  { 
    path: '/reports', 
    title: '数据报表', 
    icon: 'DataAnalysis',
    gradient: 'linear-gradient(135deg, #30cfd0 0%, #330867 100%)'
  }
]

// 快捷操作
const quickActions = [
  { key: 'newContract', title: '新建合同', icon: 'DocumentAdd', color: '#409eff' },
  { key: 'newAsset', title: '新增资产', icon: 'Plus', color: '#67c23a' },
  { key: 'quickSearch', title: '快速搜索', icon: 'Search', color: '#e6a23c' },
  { key: 'settings', title: '系统设置', icon: 'Setting', color: '#909399' }
]

// 获取子项位置（圆形分布）
const getItemPosition = (index) => {
  const radius = 120 // 半径
  const angleStep = 360 / menuItems.length
  const angle = (angleStep * index - 90) * (Math.PI / 180) // 转换为弧度，-90度使第一个在顶部
  
  const x = Math.cos(angle) * radius
  const y = Math.sin(angle) * radius
  
  return {
    transform: `translate(${x}px, ${y}px)`,
    transitionDelay: `${index * 0.05}s`
  }
}

// 切换展开
const toggleExpand = () => {
  if (!dragging.value) {
    expanded.value = !expanded.value
  }
}

// 拖拽
const handleDragStart = (event) => {
  if (expanded.value) return // 展开状态不允许拖拽
  
  dragging.value = true
  dragOffset.value = {
    x: event.clientX - position.value.x,
    y: event.clientY - position.value.y
  }
  
  document.addEventListener('mousemove', handleDragMove)
  document.addEventListener('mouseup', handleDragEnd)
}

const handleDragMove = (event) => {
  if (dragging.value) {
    position.value = {
      x: event.clientX - dragOffset.value.x,
      y: event.clientY - dragOffset.value.y
    }
  }
}

const handleDragEnd = () => {
  dragging.value = false
  document.removeEventListener('mousemove', handleDragMove)
  document.removeEventListener('mouseup', handleDragEnd)
  
  // 吸附到边缘
  const threshold = 50
  if (position.value.x < threshold) {
    position.value.x = 20
  } else if (position.value.x > window.innerWidth - threshold) {
    position.value.x = window.innerWidth - 70
  }
  
  if (position.value.y < threshold) {
    position.value.y = 20
  } else if (position.value.y > window.innerHeight - threshold) {
    position.value.y = window.innerHeight - 70
  }
}

// 点击菜单项
const handleItemClick = (item) => {
  router.push(item.path)
  expanded.value = false
  ElMessage.success(`打开 ${item.title}`)
}

// 快捷操作
const handleQuickAction = (action) => {
  switch (action.key) {
    case 'newContract':
      router.push('/contract/create')
      break
    case 'newAsset':
      router.push('/asset/create')
      break
    case 'quickSearch':
      // 触发快速搜索
      break
    case 'settings':
      router.push('/system/settings')
      break
  }
  showQuickPanel.value = false
}
</script>

<style scoped>
.floating-toolball {
  position: fixed;
  z-index: 2000;
}

/* 主悬浮球 */
.toolball-main {
  position: fixed;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  box-shadow: 0 4px 16px rgba(102, 126, 234, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s;
  z-index: 2001;
}

.toolball-main:hover {
  transform: scale(1.1);
  box-shadow: 0 6px 24px rgba(102, 126, 234, 0.5);
}

.toolball-main.is-expanded {
  background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
}

.toolball-icon {
  font-size: 24px;
  color: white;
  transition: all 0.3s;
}

.toolball-icon.rotate {
  transform: rotate(45deg);
}

.toolball-ripple {
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  border: 2px solid rgba(102, 126, 234, 0.5);
  animation: ripple 2s infinite;
}

@keyframes ripple {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  100% {
    transform: scale(1.5);
    opacity: 0;
  }
}

/* 子菜单项 */
.toolball-item {
  position: fixed;
  z-index: 2000;
}

.item-button {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: white;
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
  cursor: pointer;
  transition: all 0.3s;
}

.item-button:hover {
  transform: scale(1.15);
  box-shadow: 0 6px 20px rgba(0,0,0,0.3);
}

/* 气泡动画 */
.bubble-enter-active,
.bubble-leave-active {
  transition: all 0.3s;
}

.bubble-enter-from,
.bubble-leave-to {
  transform: scale(0);
  opacity: 0;
}

/* 快捷面板 */
.quick-panel {
  position: fixed;
  right: 100px;
  bottom: 120px;
  width: 320px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.15);
  z-index: 1999;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid #e4e7ed;
}

.panel-header h4 {
  margin: 0;
  font-size: 16px;
  color: #303133;
}

.panel-content {
  padding: 16px;
}

.quick-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.quick-action {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 16px;
  border-radius: 8px;
  border: 1px solid #e4e7ed;
  cursor: pointer;
  transition: all 0.3s;
}

.quick-action:hover {
  border-color: #409eff;
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.2);
  transform: translateY(-3px);
}

.action-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: white;
}

.quick-action span {
  font-size: 13px;
  color: #303133;
}

/* 滑动动画 */
.slide-left-enter-active,
.slide-left-leave-active {
  transition: all 0.3s;
}

.slide-left-enter-from,
.slide-left-leave-to {
  transform: translateX(100%);
  opacity: 0;
}
</style>

